package com.housediyadmin.view.titlewindow
{   	
	import com.housediyadmin.events.sizeEvent.AddSizesEvent;
	import com.housediyadmin.model.GoodsModel;
	import com.housediyadmin.model.SizeModel;
	import com.housediyadmin.vo.Size;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.binding.utils.BindingUtils;
	import mx.events.CloseEvent;
	import mx.managers.PopUpManager;
	
	import org.robotlegs.mvcs.Mediator;
	
	import spark.events.IndexChangeEvent;
	
	
	/**
	 * 
	 * @author yanghanbing
	 * 2011/2/15
	 */ 
	public class SelectSizeMediator extends Mediator
	{
		[Inject]
		public var view:SelectSizeView;
		
		[Inject]
		public var sizeModel:SizeModel;
		
		[Inject]
		public var goodsModel:GoodsModel;
		
		override public function onRegister():void
		{
			BindingUtils.bindProperty(view.ownSizes, "dataProvider", sizeModel, "addSizes");
			view.previewBtn.addEventListener(MouseEvent.CLICK, prePopUpHandler);
			view.nextBtn.addEventListener(MouseEvent.CLICK, nextPopUpHandler);
			view.cancleBtn.addEventListener(MouseEvent.CLICK, closeHandler);
			view.addEventListener(CloseEvent.CLOSE, closeHandler);
			view.done.addEventListener(MouseEvent.CLICK, addSizeHandler);
			view.ownSizes.addEventListener(IndexChangeEvent.CHANGE, showSizeInfoHandler);
		}
		
		private function addSizeHandler(event:MouseEvent):void
		{
			var size:Size = new Size();
			size.height = Number(view.heightTxt.text);
			size.isDefault = view.isDefaultCb.selected;
			size.length = Number(view.lengthTxt.text);
			size.packageSize = view.packageSizeTxt.text;
			size.packageWeight = view.packageWeightTxt.text;
			size.width = Number(view.widthTxt.text);
			size.sizeName = view.sizeNameTxt.text;
			size.sizeCreateDate = new Date();
			size.sizeEditDate = new Date();
			size.modelID = 0;
			size.goodsID = goodsModel.currentGoods.goodsID;
			if(view.ownSizes.selectedItem == null)
			{
				sizeModel.addSizes.addItem(size);
			}
			else
			{
				var index:int = sizeModel.addSizes.getItemIndex(view.ownSizes.selectedItem);
				sizeModel.addSizes.setItemAt(size, index);
			}
			view.heightTxt.text = null;
			view.isDefaultCb.selected = false;
			view.lengthTxt.text = null;
			view.packageSizeTxt.text = null;
			view.packageWeightTxt.text = null;
			view.widthTxt.text = null;
			view.sizeNameTxt.text = null;
		}
		
		private function showSizeInfoHandler(event:IndexChangeEvent):void
		{
			var size:Size = view.ownSizes.selectedItem as Size;
			view.sizeNameTxt.text = size.sizeName;
			view.packageSizeTxt.text = size.packageSize;
			view.packageWeightTxt.text = size.packageWeight;
			view.lengthTxt.text = size.length.toString();
			view.widthTxt.text = size.width.toString();
			view.heightTxt.text = size.height.toString();
			view.isDefaultCb.selected = size.isDefault;
		}
		
		private function prePopUpHandler(event:MouseEvent):void
		{
			PopUpManager.removePopUp(view);
			mediatorMap.removeMediator( this );
			var popUp:UploadGoodsPicView = new UploadGoodsPicView();
			PopUpManager.addPopUp(popUp, view, true);
			mediatorMap.createMediator(popUp);
			PopUpManager.centerPopUp(popUp);
		}
		
		private function nextPopUpHandler(event:MouseEvent):void
		{
			dispatch(new AddSizesEvent(AddSizesEvent.ADD_SIZES, sizeModel.addSizes));
			PopUpManager.removePopUp(view);
			mediatorMap.removeMediator( this );
			var popUp:SelectColorView = new SelectColorView();
			PopUpManager.addPopUp(popUp, view, true);
			mediatorMap.createMediator(popUp);
			PopUpManager.centerPopUp(popUp);
		}
		
		private function closeHandler(event:Event):void
		{
			PopUpManager.removePopUp(view);
			mediatorMap.removeMediator( this );
		}
	}
}